package com.liam.rpc.client;

import com.liam.rpc.server.RpcRequest;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

@AllArgsConstructor
@Setter
@Getter
public class RpcNetTransport {

    private String host;
    private int port;


    public Object sendRequest(RpcRequest rpcRequest){

        Socket socket = null;
        ObjectOutputStream outputStream = null;
        ObjectInputStream inputStream = null;
        try{
            socket = new Socket(host,port);
            outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(rpcRequest);
            outputStream.flush();

            inputStream = new ObjectInputStream(socket.getInputStream());
            return inputStream.readObject();

        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }finally {
            // 关闭
        }
        return null;
    }

}
